package cn.zh.stan.activity;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cn.zh.stan.BaseUrl;
import cn.zh.stan.ITem.ReItem;
import cn.zh.stan.MyApplication;
import cn.zh.stan.R;
import cn.zh.stan.adapter.ReAdapter;
import cn.zh.stan.util.MyListView;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class ReAdrass extends BaseActivity {
    ImageView rea_back;
    MyListView mlist;
    ReAdapter reAdapter=null;
    ArrayList<ReItem> reItems=new ArrayList<ReItem>();
    MyApplication mapp;
    String uid;
    LinearLayout address;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_re_adrass);
        mapp= (MyApplication) getApplication();
        uid=mapp.getSp().getString("uid","");
        inttv();
        }

    public void inttv(){
        rea_back= (ImageView) findViewById(R.id.rea_back);
        mlist= (MyListView) findViewById(R.id.mlist);
        address= (LinearLayout) findViewById(R.id.address);
        address.setOnClickListener(this);
        reAdapter=new ReAdapter(ReAdrass.this,reItems);
        mlist.setAdapter(reAdapter);
        rea_back.setOnClickListener(this);
        mlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent=new Intent(ReAdrass.this,FilOrder.class);
                intent.putExtra("AI",reItems.get(i));
                setResult(0,intent);
                finish();
              }
            });
         }


    @Override
    protected void onResume() {
        super.onResume();
        getAdrss();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id=view.getId();
        switch (id){
            case R.id.rea_back:
                finish();
                break;
            case R.id.address:
                Intent intent=new Intent(ReAdrass.this,AddAdress.class);
                intent.putExtra("type","0");
                startActivity(intent);
                break;
             }
           }


           public void getAdrss(){
               reItems.clear();
               OkHttpUtils.get(BaseUrl.adress_list)
                    .params("userId",uid)
                       .execute(new StringCallback() {
                           @Override
                           public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                             //获取用户地址列表
                               try {
                                   JSONObject js=new JSONObject(s);
                                   boolean State=js.getBoolean("State");
                                   if (State){
                                       JSONArray ja=js.getJSONArray("Res");
                                       for (int i=0;i<ja.length();i++){
                                        JSONObject jo=ja.getJSONObject(i);

                                          String Id =jo.getString("Id");
                                          String MemberId =jo.getString("MemberId");
                                          String ProvinceId =jo.getString("ProvinceId");
                                          String ProvinceName =jo.getString("ProvinceName");
                                          String CityId =jo.getString("CityId");
                                          String CityName =jo.getString("CityName");
                                          String CountyId =jo.getString("CountyId");
                                          String CountyName =jo.getString("CountyName");
                                          String CompanyName =jo.getString("CompanyName");
                                          String PersonName =jo.getString("PersonName");
                                          String Address =jo.getString("Address");
                                          String Moblie =jo.getString("Moblie");
                                          String IsDefault =jo.getString("IsDefault");
                                          String CreateTime =jo.getString("CreateTime");
                                           ReItem reItem=new ReItem(Id,MemberId,ProvinceId,ProvinceName,
                                                   CityId,CityName,CountyId,CountyName,CompanyName,PersonName,
                                                   Address,Moblie,IsDefault,CreateTime);
                                           reItems.add(reItem);
                                           }
                                       reAdapter.notifyDataSetChanged();
                                   }
                               } catch (JSONException e) {
                                   e.printStackTrace();
                               }
                           }
                           @Override
                           public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                               super.onError(isFromCache, call, response, e);
                           }
                       });
                   }


}
